const app = getApp();
const api = app.globalData.api

Page({
    data: {
        result:{},
        outputCurve: {
            lazyLoad: true
        },
        materialCurve: {
            lazyLoad: true
        },
    },
    onLoad: async function () {
        this.dataBoard()
        this.getOutputStat()
        this.materialStat()
        this.hourlyIeceRate()
        this.completionRate()
    },
    // 各项数据
    async dataBoard() {
        let res = await api.curlGet('?cla=machiningStatistics&fun=dataBoard')
        if (res.code === 1) {
            this.setData({
                result: res.data
            })
        } else {
            api.showToast(res.warn)
        }
    },
    // 获取统计数据
    getOutputStat: async function (params = {}) {
        let that = this,
            res = await api.curlPost('?cla=machiningStatistics&fun=outputStat', params)
        if (res.code === 1) {
            return api.wxCharts(that.selectComponent('#outputCurve'), api.lineChart({
                title: res.data.title,
                series: res.data.series,
                xData: res.data.data,
            }))
        }
    },
    // 获取统计数据
    materialStat: async function (params = {}) {
        let that = this,
            res = await api.curlPost('?cla=machiningStatistics&fun=material', params)
        if (res.code === 1) {
            return api.wxCharts(that.selectComponent('#materialCurve'), api.lineChart({
                title: res.data.title,
                series: res.data.series,
                xData: res.data.data,
            }))
        }
    },
    // 获取统计数据
    hourlyIeceRate: async function (params = {}) {
      let that = this,
          res = await api.curlPost('?cla=machiningStatistics&fun=hourlyIeceRate', params)
      if (res.code === 1) {
          return api.wxCharts(that.selectComponent('#hourlyIeceRateCurve'), api.lineChart({
              title: res.data.title,
              series: res.data.series,
              xData: res.data.data,
          }))
      }
    },
    // 获取统计数据
    completionRate: async function (params = {}) {
      let that = this,
          res = await api.curlPost('?cla=machiningStatistics&fun=completionRate', params)
      if (res.code === 1) {
          return api.wxCharts(that.selectComponent('#completionRateCurve'), api.lineChart({
              title: res.data.title,
              series: res.data.series,
              xData: res.data.data,
          }))
      }
  },
    onSearch: async function (e) {
        if (e.detail) {
            this.list({orderNo: e.detail})
        }
    },
    onDetail: function (obj) {
        let data = obj.currentTarget.dataset
        if (data.id) {
            wx.navigateTo({
                url: 'detail?id=' + data.id,
            })
        }
    },
    onScan: async function (e) {
        wx.scanCode({
            onlyFromCamera: true,
            success(res) {
                console.log(res)
                // if (res.errMsg === "scanCode:ok") {
                //     const string = res.result
                //     let array = api.parseStr(string)
                //     if (['saleOrderOther', 'saleOrder'].includes(array['source'])) {
                //         wx.navigateTo({
                //             url: 'detailOther?id=' + array['id'],
                //         })
                //     }
                // }
            }
        })
    },
});
